<template>
	<!-- 模板部分保持不变 -->
	<view class="loginBody">
		<view class="back">
			<view class="logo">
				<image src="@/static/images/logo.png" alt="" />
			</view>
			<view class="form">
				<!-- 手机号一键登录 -->
				<div v-if="oneClickLoginShow">
					<view class="phoneNumber">
						<span>{{ formatPhoneNumber(phoneNumber) }}</span>
					</view>
					<div class="logBtn phone">
						<u-button type="" :text="$t('instantLogin')" @click="phoneLogin" :loading="phoneLoginLoading"
							:loadingText="$t('instantLogin')"></u-button>
					</div>
				</div>
				<!-- 手机/邮箱验证码登录 -->
				<div class="Captcha" v-if="numberLoginShow">
					<div class="phoneFrom">
						<up-form labelPosition="left" :model="model1" :rules="rules" ref="form1">
							<up-form-item v-if="subsectionCurrent === 0" class="phoneNumber1" label=""
								prop="userInfo.number" borderBottom ref="item1">
								<u-input type="number" v-model="model1.userInfo.number"
									:placeholder="$t('Pleaseenternumber')" style="flex: 1;"
									@input="handlePhoneInput"></u-input>
							</up-form-item>
							<up-form-item v-else class="phoneNumber1" label="" prop="userInfo.email" borderBottom
								ref="item1">
								<u-input type="text" v-model="model1.userInfo.email"
									:placeholder="$t('Pleaseenteremail')" style="flex: 1;"
									@input="handlePhoneInput"></u-input>
							</up-form-item>
							<up-form-item class="sendCode1" label="" prop="userInfo.sendCode" borderBottom ref="item1">
								<u-input v-model="model1.userInfo.sendCode" :placeholder="$t('inputCaptcha')"
									:focus="isFocus" style="flex: 1;" @blur="isBlur"></u-input>
								<u-button style="width: 6rem;font-size: 0.75rem;" :disabled="isDisabled"
									@click="handSendCode" :loading="SendCodeLoading">{{
										buttonText }}</u-button>
							</up-form-item>
						</up-form>
						<div class="logBtn">
							<u-button type="" :text="$t('login')" @click="phoneFromLogin"
								:loading="phoneFromLoginLoading" :loadingText="$t('login')"></u-button>
							<!-- <div class="btn2">
								<u-button v-if="subsectionCurrent === 0" type="" text="使用邮箱登录"
									@click="useEmail"></u-button>
								<u-button v-else type="" text="使用手机号登录" @click="usePhome"></u-button>
							</div> -->
						</div>
					</div>
				</div>
				<!-- 账号密码登录 -->
				<div class="account" v-if="accountLoginShow">
					<up-form labelPosition="left" :model="model1" :rules="rules" ref="form2">
						<up-form-item class="phoneNumber2" label="" prop="codeInfo.number" borderBottom ref="item1">
							<u-input v-model="model1.codeInfo.number" :placeholder="$t('Pleaseenteraccount')"
								style="flex: 1;"></u-input>
						</up-form-item>
						<up-form-item class="phoneNumber2 passwordBox" label="" prop="codeInfo.possWord" borderBottom
							ref="item1">
							<u-input :password="!passShow" v-model="model1.codeInfo.possWord"
								:placeholder="$t('pleaseenterpassword')" style="flex: 1;"></u-input>
							<div class="eyeBox">
								<up-icon name="eye-fill" size="22" v-if="passShow"
									@click="passShow = !passShow"></up-icon>
								<up-icon name="eye-off" size="22" v-else @click="passShow = !passShow"></up-icon>
							</div>
						</up-form-item>
					</up-form>
					<u-button style="margin-top: 30rpx;" type="" :text="$t('login')" @click="codeFromLogin"
						:loading="codeFromLoginLoading" :loadingText="$t('login')"></u-button>
				</div>

				<div class="textBox">
					<span class="head">{{ $t('agree') }}<span class="head2" @click="toAgreement">{{ $t('UserAgreement')
					}}</span>{{ $t('and') }}<span class="head2" @click="toPolicy">{{ $t('PrivacyPolicy')
							}}</span></span>
					<!-- <span class="head">{{ $t('Unregisteredaccount') }}</span> -->
					<u-checkbox-group placement="column" v-model="checkbox" @change="handChange">
						<u-checkbox name="1" activeColor="#de868f">
						</u-checkbox>
					</u-checkbox-group>
				</div>
			</view>
			<div class="footer">
				<div class="btnList">
					<div class="btnItem">
						<image src="@/static/images/WeChat.png" @click="WeChatLogin" alt="" />
						<p>{{ $t('WeChatlogin') }}</p>
					</div>
					<!-- <div class="btnItem" v-if="networkType !== 'wifi'" @click="shortcut"> -->
					<div class="btnItem" v-if="ifOneClick" @click="shortcut">
						<image src="@/static/images/kjdl.png" alt="" />
						<p>{{ $t('instantLogin') }}</p>
					</div>
					<div class="btnItem" @click="numEm">
						<image src="@/static/images/yzm.png" alt="" />
						<p>{{ $t('captchaLogin') }}</p>
					</div>
					<div class="btnItem" @click="accountLogin">
						<image src="@/static/images/yj.png" alt="" />
						<p>{{ $t('accountlogin') }}</p>
					</div>
				</div>
			</div>
		</view>
		<upModal :modalWidth="'14.75rem'" :modalShow="modalShow" :modalTitle="modalTitle" :modalText1="modalText1"
			:isAgreement="isAgreement" :isAgreement2="isAgreement2" :showImage="showImage" :textCancel="textCancel"
			:isCancel="isCancel" :cloosBtnText="cloosBtnText" @cancel="cancel" @cloos="cloos">
		</upModal>
		<up-toast ref="uToastRef"></up-toast>
		<!-- 忘记密码 -->
		<div class="textBtn" v-if="accountLoginShow">
			<span @click="forgetJump(true)">{{ $t('ForgotPassword') }}?</span>
		</div>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getPhoneCode, getEmailCode, emailCodeLogin, wechatLogin, phoneCodeLogin, fastLogin, accountAndPwd, phoneAndPwd } from '@/api/login'
import upModal from '../../components/upModal.vue'
import { onShow, onLoad } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
// 响应式数据
const uToastRef = ref(null)
const phoneNumber = ref('')
const checkbox = ref([])
const networkType = ref('') // 网络类型
const cloosBtnText = ref(t('havereadandagreeto'))
const subsectionCurrent = ref(0)
const ifOneClick = ref(false)// 是否一键登录
let isFocus = ref(false)// 验证码输入框获取焦点
const isBlur = () => {// 验证码输入框失去焦点
	isFocus.value = false
}
const model1 = reactive({
	userInfo: {
		number: '',
		sendCode: '',
	},
	codeInfo: {
		number: '',
		possWord: '',
	},
})
const rules = reactive({
	'userInfo.number': [
		{
			required: true,
			message: t('Mobilephonenumbercannotempty'),
			trigger: ['blur',]
		},
		{
			validator: (rule, value, callback) => {
				return uni.$u.test.mobile(value) // 使用uView内置手机号校验
			},
			message: t('Themobilephonenumberincorrect'),
			trigger: ['blur',]
		}
	],
	'userInfo.email': [
		{
			required: true,
			message: t('Mailboxcannotbeempty'),
			trigger: ['blur',]
		},
		{
			validator: (rule, value, callback) => {
				return uni.$u.test.email(value) // 使用uView内置邮箱校验
			},
			message: t('Incorrectmailboxformat'),
			trigger: ['blur',]
		}
	],
	'userInfo.sendCode': {
		type: 'string',
		// len: 6,
		required: true,
		message: t('inputCaptcha'),
		trigger: ['blur',]
	},
	'codeInfo.number': [
		{
			required: true,
			message: t('Accountcannotbeempty'),
			trigger: ['blur',]
		},
	],
	'codeInfo.possWord': [
		{
			required: true,
			min: 8,
			max: 16,
			message: t('passwordcannotbeempty2'),
			trigger: ['blur',]
		},
	],
})
const isDisabled = ref(false)
const buttonText = ref(t('SendCaptcha'))
const oneClickLoginShow = ref(false)
const numberLoginShow = ref(false)
const accountLoginShow = ref(true)
const phoneLoginLoading = ref(false)
const phoneFromLoginLoading = ref(false)
const SendCodeLoading = ref(false)
const codeFromLoginLoading = ref(false)
const passShow = ref(false)
const isAgreement = ref(false) // 是否用户协议内容
const isAgreement2 = ref(false) // 是否用户协议内容2
const modalShow = ref(false) // 弹窗显示状态
const showImage = ref(true) // 是否显示图片
const isCancel = ref(false) // 是否有取消按钮
const modalTitle = ref('') // 弹窗标题
const modalText1 = ref('') // 弹窗内容
const textCancel = ref(t('notagree')) // 取消按钮文字
const countdown = ref(60) // 倒计时
let intervalId = null // 定时器ID

// 方法
const shortcut = () => {// 显示一键登录
	oneClickLoginShow.value = true
	numberLoginShow.value = false
	accountLoginShow.value = false
}

const numEm = () => {// 显示验证码登录
	oneClickLoginShow.value = false
	numberLoginShow.value = true
	accountLoginShow.value = false
}

const accountLogin = () => {// 显示账号密码登录
	oneClickLoginShow.value = false
	numberLoginShow.value = false
	accountLoginShow.value = true
}

// 打开用户协议和隐私政策页面
const toAgreement = () => {
	uni.navigateTo({
		// url: `/pages/agreementBook/agreement?title=${t('userAgreement')}&urlData=http://47.119.43.199/html2/userAgreement.html`,
		url: `/pages/setting/html?index=1`,
	})
}

const toPolicy = () => {
	uni.navigateTo({
		// url: '/pages/agreementBook/policy',
		// url: `/pages/agreementBook/agreement?title=${t('privacyPolicy')}&urlData=http://47.119.43.199/html2/privacyPolicy.html`,
		url: `/pages/setting/html?index=0`,
	})
}

const phoneLogin = () => {
	if (checkbox.value.length === 0) {
		// uToastRef.value.show({
		// 	message: t('PleasecheckAgreementandPrivacy'),
		// 	duration: 2000,
		// 	type: "default",
		// 	icon: "none",
		// })
		modalShow.value = true
		showImage.value = true
		isAgreement.value = false
		isCancel.value = false
		modalText1.value = t('PleasecheckAgreementandPrivacy')
		cloosBtnText.value = t('confirm')
	} else {
		phoneLoginLoading.value = true
		// 获取手机号码
		uni.login({
			provider: 'univerify',
			univerifyStyle: {
				"fullScreen": true,
				"title": t('fastlogon'),
				"backgroundColor": "#ffffff",
				"phoneNum": {
					"color": "#000000",
					"fontSize": "18"
				},
				"slogan": {
					"color": "#8a8b90",
					"fontSize": "12"
				},
				"icon": {
					"path": "/static/logo/dpl.png"
				},
				"authButton": {
					"normalColor": "#de868f",
					"highlightColor": "#2861c5",
					"disabledColor": "#73aaf5",
					"textColor": "#ffffff",
					"title": t('Oneclicklogin')
				},
				"otherLoginButton": {
					"visible": "true",
					"normalColor": "#f8f8f8",
					"highlightColor": "#dedede",
					"textColor": "#000000",
					"title": t('Otherwaystologin'),
					"borderWidth": "1px",
					"borderColor": "#c5c5c5"
				},
				"privacyTerms": {
					"defaultCheckBoxState": "true",
					"uncheckedImage": "/static/images/nodg.png", // 可选 条款勾选框未选中状态图片（仅支持本地图片 建议尺寸 24x24px）(3.2.0+ 版本支持)
					"checkedImage": "/static/images/dg.png", // 可选 条款勾选框选中状态图片（仅支持本地图片 建议尺寸24x24px）(3.2.0+ 版本支持)
					"textColor": "#8a8b90",
					"termsColor": "#1d4788",
					"prefix": t('agree'),
					"fontSize": "12",
					"privacyItems": [
						{
							"url": "http://47.119.43.199/html2/privacyPolicy.html",
							"title": t('DameiPolicy')
						},
						{
							"url": "http://47.119.43.199/html2/userAgreement.html",
							"title": t('ServiceAgreement')
						},
					]
				}
			},
			success(res) {
				// console.error("登录成功：==res===", res)
				uniCloud.callFunction({
					name: 'dpllogin',
					data: {
						'access_token': res.authResult.access_token,
						'openid': res.authResult.openid
					},
					success(callRes) {
						// console.error("登录成功：==获取手机号===", callRes)
						phoneNumber.value = callRes.result.phoneNumber
						let para = {
							phone: callRes.result.phoneNumber,
						}
						fastLogin(para).then((res) => {
							// console.log('登录', res)
							if (res.code === 200) {
								let loginData = {
									token: res.data.tokenList.accessToken,
									refreshToken: res.data.tokenList.refreshToken,
									accessTokenExp: res.data.tokenList.accessTokenExp,
									user: res.data.user,
									phone: res.data.phone,
								}
								sendAndLoginData(loginData)
								// 关闭所有页面，进入首页
								uni.reLaunch({
									url: '/pages/index/index',
								});
							} else if (res.code == 981) {
								modalShow.value = true
								showImage.value = true
								isAgreement.value = false
								isCancel.value = false
								modalText1.value = res.msg
								cloosBtnText.value = t('confirm')
							} else {
								uToastRef.value.show({
									message: `${res.msg}`,
									duration: 2000,
									type: "default",
									icon: "none",
								})
							}
						})
					},
					fail(callErr) {
						// console.log('调用云函数出错' + callErr)
						uni.closeAuthView()
					},
					complete() {
						uni.closeAuthView()
					}
				})
				phoneLoginLoading.value = false
			},
			fail(err) {
				// console.log('error====', err)
				numEm()
				uni.closeAuthView()
				phoneLoginLoading.value = false
			}
		})
	}
}
const form1 = ref(null)
// 验证码登录
const phoneFromLogin = () => {
	if (checkbox.value.length === 0) {
		// uToastRef.value.show({
		// 	message: t('PleasecheckAgreementandPrivacy'),
		// 	duration: 2000,
		// 	type: "default",
		// 	icon: "none",
		// })
		modalShow.value = true
		showImage.value = true
		isAgreement.value = false
		isCancel.value = false
		modalText1.value = t('PleasecheckAgreementandPrivacy')
		cloosBtnText.value = t('confirm')
	} else {
		form1.value.validate().then(valid => {
			if (valid) {
				phoneFromLoginLoading.value = true
				if (subsectionCurrent.value === 0) {
					let para = {
						phone: model1.userInfo.number,
						msg: model1.userInfo.sendCode
					}
					phoneCodeLogin(para).then(res => {
						if (res.code === 200) {
							let loginData = {
								token: res.data.tokenList.accessToken,
								refreshToken: res.data.tokenList.refreshToken,
								accessTokenExp: res.data.tokenList.accessTokenExp,
								user: res.data.user,
								phone: res.data.phone,
							}
							sendAndLoginData(loginData)
							// 关闭所有页面，进入首页
							uni.reLaunch({
								url: '/pages/index/index',
							});
						} else if (res.code == 981) {
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = res.msg
							cloosBtnText.value = t('confirm')
						} else if (res.code === 11122005) {// 验证码错误
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('Captchaerrorenteragain')
							cloosBtnText.value = t('confirm')
						} else if (res.code === 11122004) {// 验证码过期或不存在
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('codehasexpiredresendit')
							cloosBtnText.value = t('confirm')
						} else {
							uToastRef.value.show({
								message: `${res.msg}`,
								duration: 2000,
								type: "default",
								icon: "none",
							})
						}
					}).finally(() => {
						phoneFromLoginLoading.value = false
					})
				} else {
					let para = {
						email: model1.userInfo.email,
						msg: model1.userInfo.sendCode
					}
					emailCodeLogin(para).then(res => {
						if (res.code === 200) {
							let loginData = {
								token: res.data.tokenList.accessToken,
								refreshToken: res.data.tokenList.refreshToken,
								accessTokenExp: res.data.tokenList.accessTokenExp,
								user: res.data.user,
								phone: res.data.phone,
							}
							sendAndLoginData(loginData)
							// 关闭所有页面，进入首页
							uni.reLaunch({
								url: '/pages/index/index',
							});
						} else if (res.code == 981) {
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = res.msg
							cloosBtnText.value = t('confirm')
						} else if (res.code === 11122005) {// 验证码错误
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('Captchaerrorenteragain')
							cloosBtnText.value = t('confirm')
						} else if (res.code === 11122004) {// 验证码过期或不存在
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('codehasexpiredresendit')
							cloosBtnText.value = t('confirm')
						} else {
							uToastRef.value.show({
								message: `${res.msg}`,
								duration: 2000,
								type: "default",
								icon: "none",
							})
						}
					}).finally(() => {
						phoneFromLoginLoading.value = false
					})
				}
			}
		})
	}
}

// 存储登录信息
const sendAndLoginData = (data) => {
	// console.log('储存信息', data);

	if (data) {
		uni.setStorageSync('token', data.token)
		uni.setStorageSync('refreshToken', data.refreshToken)
		uni.setStorageSync('accessTokenExp', data.accessTokenExp)
		uni.setStorageSync('avatarUrl', data.user.avatarUrl)
		uni.setStorageSync('username', data.user.username)
		uni.setStorageSync('userCode', data.user.user_code)
		uni.setStorageSync('dplID', data.user.dplID)
		uni.setStorageSync('userPhone', data.phone)
	}
}

const useEmail = () => {
	subsectionCurrent.value = 1
}

const usePhome = () => {
	subsectionCurrent.value = 0
}

// 微信一键登录
const WeChatLogin = async () => {
	if (checkbox.value.length === 0) {
		// uToastRef.value.show({
		// 	message: t('PleasecheckAgreementandPrivacy'),
		// 	duration: 2000,
		// 	type: "default",
		// 	icon: "none",
		// })
		modalShow.value = true
		showImage.value = true
		isAgreement.value = false
		isCancel.value = false
		modalText1.value = t('PleasecheckAgreementandPrivacy')
		cloosBtnText.value = t('confirm')
		return;
	}
	let loginResult = await uni.login({
		provider: 'weixin',
		onlyAuthorize: true
	});
	if (loginResult.errMsg !== 'login:ok') {
		uToastRef.value.show({
			message: t('Loginagain'),
			duration: 2000,
			type: "default",
			icon: "none",
		})
		return
	};
	if (loginResult.code) {
		wechatLogin({ wxcode: loginResult.code }).then(res => {
			// console.log("🚀 微信登录 res:", res)
			if (res.code == 200) {
				let loginData = {
					token: res.data.tokenList.accessToken,
					refreshToken: res.data.tokenList.refreshToken,
					accessTokenExp: res.data.tokenList.accessTokenExp,
					user: res.data.user,
					phone: res.data.phone,
				}
				sendAndLoginData(loginData)
				// 关闭所有页面，进入首页
				uni.reLaunch({
					url: '/pages/index/index',
				});
			} else if (res.code == 981) {
				modalShow.value = true
				showImage.value = true
				isAgreement.value = false
				isCancel.value = false
				modalText1.value = res.msg
				cloosBtnText.value = t('confirm')
			} else {
				uToastRef.value.show({
					message: res.msg,
					duration: 2000,
					type: "default",
					icon: "none",
				})
			}
		});
	}
}

const handother = () => {
	// 待实现
}

const formatPhoneNumber = (phone) => {
	if (phone && phone.length >= 11) {
		return phone.slice(0, 3) + '****' + phone.slice(7);
	} else {
		return phone;
	}
}

const handChange = (n) => {
	// console.log('change', n, checkbox.value);
}

// 获取网络状态
const getNetworkType = () => {
	uni.getNetworkType({
		success: (res) => {
			networkType.value = res.networkType
			if (networkType.value !== 'wifi') {
				oneClickLoginShow.value = true
				numberLoginShow.value = false
				accountLoginShow.value = false
			} else {
				oneClickLoginShow.value = false
				numberLoginShow.value = true
				accountLoginShow.value = false
			}
		}
	});
}

const handlePhoneInput = () => {
	// 待实现
}

// 获取验证码按钮
const handSendCode = () => {
	if (isDisabled.value) return;
	if (subsectionCurrent.value === 0) {
		const phoneReg = /^1[3-9]\d{9}$/
		if (!model1.userInfo.number) {
			uToastRef.value.show({
				message: t('Pleaseenternumber'),
				duration: 2000,
				type: "default",
				icon: "none",
			})
			return
		}
		if (!phoneReg.test(model1.userInfo.number)) {
			uToastRef.value.show({
				message: t('Pleaseentercorrectemailnumber'),
				duration: 2000,
				type: "default",
				icon: "none",
			})
			return
		}
		isFocus.value = true
		SendCodeLoading.value = true;
		getPhoneCode({ phone: model1.userInfo.number }).then(res => {
			if (res.code === 200) {
				isDisabled.value = true;
				buttonText.value = '60s';
				countdown.value = 60;
				startCountdown();
			} else {
				uToastRef.value.show({
					message: `${res.msg}`,
					duration: 2000,
					type: "default",
					icon: "none",
				})
			}
		}).catch(err => {
			uToastRef.value.show({
				message: `${err.msg}`,
				duration: 2000,
				type: "default",
				icon: "none",
			})
		}).finally(() => {
			SendCodeLoading.value = false
		})
	} else if (subsectionCurrent.value === 1) {
		const emailReg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
		if (!model1.userInfo.email) {
			uToastRef.value.show({
				message: t('Pleaseenteremail'),
				duration: 2000,
				type: "default",
				icon: "none",
			})
			return
		}
		if (!emailReg.test(model1.userInfo.email)) {
			uToastRef.value.show({
				message: t('Pleaseentercorrectemailaddress'),
				duration: 2000,
				type: "default",
				icon: "none",
			})
			return;
		}
		SendCodeLoading.value = true;
		getEmailCode(model1.userInfo.email).then(res => {
			if (res.code === 200) {
				isDisabled.value = true;
				buttonText.value = '60s';
				countdown.value = 60;
				startCountdown();
			} else {
				uToastRef.value.show({
					message: `${res.msg}`,
					duration: 2000,
					type: "default",
					icon: "none",
				})
			}
		}).catch(err => {
			uToastRef.value.show({
				message: `${err.msg}`,
				duration: 2000,
				type: "default",
				icon: "none",
			})
		}).finally(() => {
			SendCodeLoading.value = false
		})
	}
}

// 开始倒计时
const startCountdown = () => {
	clearInterval(intervalId)
	intervalId = setInterval(() => {
		if (countdown.value <= 0) {
			clearInterval(intervalId)
			isDisabled.value = false
			buttonText.value = t('SendCaptcha')
		} else {
			countdown.value -= 1
			buttonText.value = `${countdown.value}s`
		}
	}, 1000)
}

const sectionChange = (index) => {
	subsectionCurrent.value = index
	if (index === 0) {
		model1.userInfo.email = ''
	} else {
		model1.userInfo.number = ''
	}
}

const cancel = () => {
	if (isAgreement.value) {
		isAgreement.value = false
		isAgreement2.value = true
		textCancel.value = t('Disagreeandwithdraw')
		return
	} else if (isAgreement2.value) {
		modalShow.value = false
		// 退出应用
		uni.getSystemInfo({
			success: function (res) {
				if (res.platform == 'android') {
					plus.runtime.quit()
				} else {
					plus.ios.import("UIApplication").sharedApplication().performSelector("exit")
				}
			}
		})
	}
}

const cloos = () => {
	if (isAgreement.value || isAgreement2.value) {
		checkbox.value.push('1')
	}
	modalTitle.value = ''// 清空标题
	modalShow.value = false
	uni.setStorageSync("firstTime", '1')
}
const form2 = ref(null)
// 账号/手机号密码登录按钮
const codeFromLogin = () => {
	if (checkbox.value.length === 0) {
		// uToastRef.value.show({
		// 	message: t('PleasecheckAgreementandPrivacy'),
		// 	duration: 2000,
		// 	type: "default",
		// 	icon: "none",
		// })
		modalShow.value = true
		showImage.value = true
		isAgreement.value = false
		isCancel.value = false
		modalText1.value = t('PleasecheckAgreementandPrivacy')
		cloosBtnText.value = t('confirm')
	} else {
		form2.value.validate().then(valid => {
			if (valid) {
				codeFromLoginLoading.value = true
				if (model1.codeInfo.number.length === 11) {// 手机号密码登录
					let para = {
						phone: model1.codeInfo.number,
						password: model1.codeInfo.possWord,
					}
					phoneAndPwd(para).then((res) => {
						if (res.code == 200) {
							let loginData = {
								token: res.data.tokenList.accessToken,
								refreshToken: res.data.tokenList.refreshToken,
								accessTokenExp: res.data.tokenList.accessTokenExp,
								user: res.data.user,
								phone: res.data.phone,
							}
							sendAndLoginData(loginData)
							// 关闭所有页面，进入首页
							uni.reLaunch({
								url: '/pages/index/index',
							});
						} else if (res.code == 10102008) {// 手机号或密码不正确
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('Loginphoneandpassword')
							cloosBtnText.value = t('confirm')
						} else if (res.code == 981) {// 账号被封禁
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('Accountbeenblocked')
							cloosBtnText.value = t('confirm')
						} else {
							uToastRef.value.show({
								message: `${res.msg}`,
								duration: 2000,
								type: "default",
								icon: "none",
							})
						}
					}).finally(() => {
						codeFromLoginLoading.value = false
					})
				} else {// 账号密码登录
					let para = {
						account: model1.codeInfo.number,
						password: model1.codeInfo.possWord,
					}
					accountAndPwd(para).then((res) => {
						if (res.code == 200) {
							let loginData = {
								token: res.data.tokenList.accessToken,
								refreshToken: res.data.tokenList.refreshToken,
								accessTokenExp: res.data.tokenList.accessTokenExp,
								user: res.data.user,
								phone: res.data.phone,
							}
							sendAndLoginData(loginData)
							// 关闭所有页面，进入首页
							uni.reLaunch({
								url: '/pages/index/index',
							});
						} else if (res.code == 10102009) {// 用户未注册
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('Loginaccountandpassword')
							cloosBtnText.value = t('confirm')
						} else if (res.code == 981) {
							modalShow.value = true
							showImage.value = true
							isAgreement.value = false
							isCancel.value = false
							modalText1.value = t('Accountbeenblocked')
							cloosBtnText.value = t('confirm')
						} else {
							uToastRef.value.show({
								message: `${res.msg}`,
								duration: 2000,
								type: "default",
								icon: "none",
							})
						}
					}).finally(() => {
						codeFromLoginLoading.value = false
					})
				}
				// 延迟10秒
				setTimeout(() => {
					codeFromLoginLoading.value = false
				}, 10000)
			}
		})
	}
}

const forgetJump = (data) => {
	if (data) {
		uni.navigateTo({
			url: `/pages/login/ForgotPassword?forget=${t('ForgotPassword')}`,
		})
	} else {
		uni.navigateTo({
			url: `/pages/login/ForgotPassword?forget=${t('newuserregistration')}`,
		})
	}
}

// 生命周期钩子
onMounted(() => {
	if (!uni.getStorageSync("firstTime")) {
		cloosBtnText.value = t('havereadandagreeto')
		modalShow.value = true
		showImage.value = false
		isAgreement.value = true
		isCancel.value = true
		modalTitle.value = t('UserAgreementPrivacyPolicy')
	}
	getNetworkType()
	// #ifndef H5
	// 预登录
	uni.preLogin({
		provider: 'univerify',
		success(res) {  //预登录成功
			console.log('预登录成功', res);
			ifOneClick.value = true
			shortcut()
		},
		fail(res) {  // 预登录失败
			// 不显示一键登录选项（或置灰）
			// 根据错误信息判断失败原因，如有需要可将错误提交给统计服务器
			console.log(res.errCode)
			console.log(res.errMsg)
			ifOneClick.value = false
			numEm()
		}
	})
	// #endif
	// if (localStorage.getItem("token")) {
	// 	uni.switchTab({
	// 		url: '/pages/index/index'
	// 	})
	// }
})

onShow(() => {
	// 可以在这里添加显示时的逻辑
})

onLoad(() => {
	// 可以在这里添加加载时的逻辑
})
</script>

<style lang="scss">
/* 样式部分保持不变 */
.loginBody {
	width: 100%;
	height: 100vh;
	// height: calc(100vh - 2rem);
	// margin-top: 2rem;
	background-size: cover;
	background-position: center;
	position: relative;
	// background: url('https://t.tutu.to/img/Ikwie') no-repeat center center;
	background: url('/static/images/background.png') no-repeat center center;
	background-size: 100% 100%;

	.logo {
		text-align: center;

		image {
			width: 240rpx;
			height: 240rpx;
		}
	}

	.form {
		.textBox {
			margin-top: 50rpx;
			text-align: center;
			font-size: 24rpx;
			position: relative;

			.head {
				width: 80%;
				color: #8b8b8b;
				margin-bottom: 10rpx;

				.head2 {
					color: #000000;
				}
			}

			span {
				display: inline-block;
			}

			.u-checkbox {
				position: absolute;
				left: 0;
				bottom: 0%;
				left: 10%;
			}
		}

		.phoneNumber {
			width: 100%;
			text-align: center;
			display: flex;
			justify-content: center;
			gap: 10rpx;

			span {
				display: inline-block;
			}

			.u-tag-wrapper {
				.u-tag {
					font-weight: 600;
					height: 22px;
				}
			}
		}

		.logBtn {
			margin: 0 auto;
			margin-top: 36rpx;
			width: 82%;
			text-align: center;

			.u-button {
				color: #fff;
				height: 80rpx;
				font-weight: 600;
				border-radius: 10px;
				background-color: #de868f;
				border: 1px solid #bbbbbb;
			}
		}

		.WeChat {
			.u-button {
				font-weight: 500;
				color: #000;
				background-color: #f4f4f4;
			}

			.iconWeChat {
				width: 40rpx;
				height: 40rpx;
				margin-right: 8rpx;
			}
		}

		.otherLogBtn {
			width: 100%;
			text-align: center;
			margin-top: 40rpx;
			color: #636363;
			font-size: 28rpx;
		}

		.Captcha {
			width: 100%;

			.up {
				width: 50%;
				margin: 0 auto;
			}

			.phoneFrom {
				width: 85%;
				margin: 0 auto;

				.phoneNumber1 {
					.u-input {
						background-color: #F2F2F2;
					}

					.u-input--radius {
						border-radius: 15px;
					}
				}

				.sendCode1 {
					.u-input {
						background-color: #F2F2F2;
					}

					.u-input--radius {
						border-radius: 15px 0 0 15px;
					}

					.u-button {
						border-radius: 0 15px 15px 0;
					}
				}

				.logBtn {
					margin: 0 auto;
					margin-top: 28rpx;
					width: 100%;
					text-align: center;

					.u-button {
						margin-bottom: 20rpx;
						color: #fff;
						height: 80rpx;
						font-weight: 600;
						border-radius: 10px;
						background-color: #de868f;
						border: 1px solid #bbbbbb;
					}

					.btn2 {
						.u-button {
							background-color: #BEBEBE;
						}
					}
				}
			}
		}
	}

	.footer {
		width: 100%;
		position: fixed;
		bottom: 50rpx;

		.btnList {
			width: 100%;
			display: flex;

			.btnItem {
				flex-grow: 1;
				text-align: center;

				p {
					color: #9A9A9A;
					font-size: 24rpx;
				}
			}

			image {
				width: 80rpx;
				height: 80rpx;
			}
		}
	}

	.back {
		width: 100%;
		position: fixed;
		top: 280rpx;
	}

	.account {
		width: 85%;
		margin: 0 auto;

		.phoneNumber2 {
			.u-input {
				width: 100%;
				background-color: #F2F2F2;
			}

			.u-input--radius {
				border-radius: 15px;
			}

			::v-deep .u-form-item__body__right__content__slot {
				width: 100%;
			}
		}

		.passwordBox {
			position: relative;

			.eyeBox {
				position: absolute;
				right: 20rpx;
			}
		}

		.u-button {
			color: #fff;
			height: 80rpx;
			font-weight: 600;
			border-radius: 10px;
			background-color: #de868f;
			border: 1px solid #bbbbbb;
		}
	}

	.textBtn {
		width: 100%;
		font-size: 14px;
		text-align: center;
		position: absolute;
		bottom: 15%;

		.forget {
			float: right;
		}
	}
}
</style>